package cn.iocoder.yudao.module.handguard.controller.admin.housingresource;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.admin.housingresource.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.housingresource.HousingResourceDO;
import cn.iocoder.yudao.module.handguard.convert.housingresource.HousingResourceConvert;
import cn.iocoder.yudao.module.handguard.service.housingresource.HousingResourceService;

@Tag(name = "管理后台 - 房源信息")
@RestController
@RequestMapping("/hutai/housing-resource")
@Validated
public class HousingResourceController {

    @Resource
    private HousingResourceService housingResourceService;

    @PostMapping("/create")
    @Operation(summary = "创建房源信息")
    @PreAuthorize("@ss.hasPermission('housing:resource:create')")
    public CommonResult<Long> createHousingResource(@Valid @RequestBody HousingResourceCreateReqVO createReqVO) {
        return success(housingResourceService.createHousingResource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新房源信息")
    @PreAuthorize("@ss.hasPermission('housing:resource:update')")
    public CommonResult<Boolean> updateHousingResource(@Valid @RequestBody HousingResourceUpdateReqVO updateReqVO) {
        housingResourceService.updateHousingResource(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateMobile")
    @Operation(summary = "更新房源客服电话")
    @PreAuthorize("@ss.hasPermission('housing:resource:update')")
    public CommonResult<Boolean> updateHousingMobile(@Valid @RequestBody HousingMobileReqVO reqVO) {
        housingResourceService.updateHousingMobile(reqVO);
        return success(true);
    }

    @GetMapping("/getHousingMobile")
    @Operation(summary = "获得房源客服电话")
    public CommonResult<String> getHousingMobile() {
        String mobile = housingResourceService.getHousingMobile();
        return success(mobile);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除房源信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('housing:resource:delete')")
    public CommonResult<Boolean> deleteHousingResource(@RequestParam("id") Long id) {
        housingResourceService.deleteHousingResource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得房源信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('housing:resource:query')")
    public CommonResult<HousingResourceRespVO> getHousingResource(@RequestParam("id") Long id) {
        HousingResourceDO housingResource = housingResourceService.getHousingResource(id);
        return success(HousingResourceConvert.INSTANCE.convert(housingResource));
    }

    @GetMapping("/list")
    @Operation(summary = "获得房源信息列表")
    @PreAuthorize("@ss.hasPermission('housing:resource:query')")
    public CommonResult<List<HousingResourceRespVO>> getHousingResourceList() {
        List<HousingResourceDO> list = housingResourceService.getHousingResourceList();
        return success(HousingResourceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得房源信息分页")
    @PreAuthorize("@ss.hasPermission('housing:resource:query')")
    public CommonResult<PageResult<HousingResourceRespVO>> getHousingResourcePage(@Valid HousingResourcePageReqVO pageVO) {
        PageResult<HousingResourceDO> pageResult = housingResourceService.getHousingResourcePage(pageVO);
        return success(HousingResourceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getHousingUpdateTime")
    @Operation(summary = "获得房源最新更新时间")
    @PreAuthorize("@ss.hasPermission('housing:resource:query')")
    public CommonResult<String> getHousingUpdateTime(@RequestParam("type") String type) {
        String time = housingResourceService.getHousingUpdateTime(type);
        return success(time);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出房源信息 Excel")
    @PreAuthorize("@ss.hasPermission('housing:resource:export')")
    @OperateLog(type = EXPORT)
    public void exportHousingResourceExcel(@Valid HousingResourceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<HousingResourceDO> list = housingResourceService.getHousingResourceList(exportReqVO);
        // 导出 Excel
        List<HousingResourceExcelVO> datas = BeanUtil.copyToList(list, HousingResourceExcelVO.class);
        datas.forEach(d ->{
            if (d.getAuctionState()==0){
                d.setAuctionStateStr("未起拍");
            } else if (d.getAuctionState()==1) {
                d.setAuctionStateStr("竞拍中");
            }else if (d.getAuctionState()==2) {
                d.setAuctionStateStr("已结束");
            }
        });
        ExcelUtils.write(response, "房源信息.xls", "数据", HousingResourceExcelVO.class, datas);
    }

}
